<%@page import="com.nebula.common.Const"%>
<%@page import="com.nebula.pojo.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
	User user = (User)session.getAttribute(Const.USERINFO);
 %>

<!DOCTYPE HTML>
<html>
<head>
<title>目录</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/zTree/v3/css/zTreeStyle/zTreeStyle.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/layer/2.4/skin/layer.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/h-ui/css/H-ui.min.css" />
<style type="text/css">
.ztree li span.button.ico_docu {  
	background-position: -110px 0;  
    margin-right: 2px;  
    vertical-align: top;  
}
.directory-div {
	 border:1px solid #a5b6c8;
	 background:#eef3f7;
	 border-radius:3px;
	 height: 80%;
	 width:99%;
	 margin:auto;
	 overflow: auto;
}
.ztree li span.button.add {
	margin-left:2px; 
	margin-right: -1px; 
	background-position:-144px 0; 
	vertical-align:top; 
	*vertical-align:middle
}
</style>
</head>
  
<body>

<input type="hidden" value="<%=user.getUsername()%>" id="user">
<input type="hidden" value="<%=request.getParameter("type")%>" id="type">
<input type="hidden" value="<%=request.getParameter("valArr")%>" id="valArr">
<input type="hidden" value="<%=request.getParameter("surl")%>" id="surl">
<div class="directory-div">
	<ul id="treeDirectory" class="ztree"></ul>
</div>
<div class="mt-5" style="margin-left: 230px">
	<input class="btn btn-primary radius" type="button" 
		value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;确定&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		id="btn_ok">
	<input class="btn btn-secondary-outline radius" type="button" 
		value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;取消&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		id="btn_exit">
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/zTree/v3/js/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/zTree/v3/js/jquery.ztree.excheck-3.5.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/zTree/v3/js/jquery.ztree.exedit-3.5.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/layer/2.4/layer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery.base64.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery.cookie.js"></script>
<script type="text/javascript">
	var index = parent.layer.getFrameIndex(window.name);
	var setting = {
		view:{
			showLine : false,
			selectedMulti: false,
			dblClickExpand: false,
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
		},
		data : {
			simpleData: {
				enable: true,
				idKey: "folderid",
				pIdKey: "parentid",
				rootPId: 0,
			},
		},
		async: {
			enable: true,
			type: "post",
			url: "/nebula/folder/get_directory.action",
			autoParam: ["folderid"],
		},
		callback: {
			onAsyncSuccess: zTreeOnAsyncSuccess,
			onMouseUp: onMouseUp
		}
	};
	
	function zTreeOnAsyncSuccess(event, treeId, treeNode,msg) {
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		var rootnode = treeObj.getNodesByFilter(function(node){
			return node.level == 0
		},true);
		if(rootnode != null){
			treeObj.expandNode(rootnode,true,false,false);
		}
	};
	
	function addHoverDom(treeId, treeNode){
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='新建文件夹' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(){
			var prompt = layer.prompt({title:'新建文件夹'},function(folderName,index){
				var i;
				layer.close(index);
				var data = {parentid:treeNode.folderid,folderName:folderName,username:$("#user").val()};
				$.ajax({
					type: "POST",
					url: "/nebula/folder/save.action",
					contentType:'application/json;charset=utf-8',
					data : JSON.stringify(data),
					dataType: "JSON",
					beforeSend: function () {  
		                i = layer.msg('正在创建文件夹', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px', time:100000}) ;  
		            },
					success : function(data){
						if(data.status == 0){
							layer.close(i);
							layer.msg(data.msg,{offset:'t',icon: 1,time:1000},function(){
								if(!treeNode.isParent){
									treeNode.isParent = true;
								}
								var zTree = $.fn.zTree.getZTreeObj(treeId),
								nodes = zTree.getSelectedNodes();
								zTree.reAsyncChildNodes(nodes[0], "refresh",false);
								zTree.expandNode(nodes[0],true,false,false);
							});
						}
					}
				});
			});
		});
	}
	
	function removeHoverDom(treeId, treeNode){
		$("#addBtn_"+treeNode.tId).unbind().remove();
	}
	
	$(document).ready(function() {
		$.fn.zTree.init($("#treeDirectory"), setting,null);
	});
	
	var folderId;
	function onMouseUp(event, treeId, treeNode){
		folderId = treeNode.folderid;
	}
	
	$("#btn_ok").click(function(){
		if($("#type").val() == "copy"){
			$.ajax({
				type: "post",
				url : "/nebula/folder/copyTo.action",
				data: {
					valArr : $("#valArr").val(),
					folderid : folderId
				},
				beforeSend: function () {  
		            i = layer.msg('正在复制文件,请稍后...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px', time:100000}) ;  
		        },
				success : function(data){
					if(data.status == 0){
						layer.close(i);
						layer.msg(data.msg,{offset:['0',''],icon: 1,time:2000},function(){
							window.parent.location.reload();
							parent.layer.close(index);
						});
					}else if(data.status == 1){
						layer.close(i);
						layer.msg(data.msg,{offset:['0',''],icon: 2,time:2000});
					}
				}
			});
		}else if($("#type").val() == "move"){
			$.ajax({
				type: "post",
				url : "/nebula/folder/moveTo.action",
				data: {
					valArr : $("#valArr").val(),
					folderid : folderId
				},
				beforeSend: function () {  
		            i = layer.msg('正在移动文件,请稍后...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px', time:100000}) ;  
		        },
				success : function(data){
					if(data.status == 0){
						layer.close(i);
						layer.msg(data.msg,{offset:['0',''],icon: 1,time:2000},function(){
							window.parent.location.reload();
							parent.layer.close(index);
						});
					}else if(data.status == 1){
						layer.close(i);
						layer.msg(data.msg,{offset:['0',''],icon: 2,time:2000});
					}
				}
			});
		}else if($("#type").val() == "save"){
			$.ajax({
				type: "post",
				url : "/nebula/folder/saveTo.action",
				data: {
					valArr : $("#valArr").val(),
					folderid : folderId
				},
				beforeSend: function () {  
		            i = layer.msg('正在保存文件,请稍后...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px', time:100000}) ;  
		        },
				success : function(data){
					if(data.status == 0){
						layer.close(i);
						$.ajax({
							type: "post",
							url : "/nebula/share/updateSaveTimes.action",
							data : {
								surl : $("#surl").val()
							},
							dataType : "json",
							success : function(data){
								console.log(data.msg);
							}
						});
						parent.layer.msg(data.msg+" 【<a id='look' href='/nebula/index.action'>点击查看</a>】"
							,{offset:['t',''],icon: 1,time:-1,closeBtn:2});
						parent.$("#folderId").val(folderId);
						parent.layer.close(index);
					}else if(data.status == 1){
						layer.close(i);
						layer.msg(data.msg,{offset:['0',''],icon: 2,time:2000});
					}
				}
			});
		}else if($("#type").val() == "fsave"){
			layer.confirm("<div style='text-align: center;padding: 25px 25px 22px'>提示：若选择保存的文件中存在\"xxx等\"的文件名，将会保存该文件下的所有文件（夹），私有云中不会创建名为\"xxx等\"的文件夹！</div>",{title:"提示",area: ['520px','230px'],skin:"mySkin"},function(index1){
				$.ajax({
					type: "post",
					url : "/nebula/folder/saveTo.action",
					data: {
						valArr : $("#valArr").val(),
						folderid : folderId
					},
					beforeSend: function () {  
			            i = layer.msg('正在保存文件,请稍后...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px', time:100000}) ;  
			        },
					success : function(data){
						if(data.status == 0){
							layer.close(i);
							parent.layer.msg(data.msg,{offset:['t',''],icon: 1,time:2000});
							parent.layer.close(index);
						}else if(data.status == 1){
							layer.close(i);
							layer.msg(data.msg,{offset:['0',''],icon: 2,time:2000});
						}
					}
				});
				layer.close(index1);
			});
		}
	});
	
	$("#btn_exit").click(function(){
		parent.layer.closeAll();
	});
	
</script>
</body>
</html>
